package datasource;


import datasource.config.C3P0Config;
import jdbc.domian.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    int pageSize=3;
public List<User> findByPage(int page){
    Connection conn=null;
    PreparedStatement ps = null;
    List<User> list = new ArrayList();
    try {
        conn = C3P0Config.getConnection();
        String sql = "select * from user order by id desc limit ?,?";
        ps=conn.prepareStatement(sql);
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        ps.executeQuery();
        ResultSet set = ps.getResultSet();
        while (set.next()){
            User user = new User();
            user.setId(set.getInt("id"));
            user.setUsername(set.getString("username"));
            user.setPassword(set.getString("password"));
            user.setEmail(set.getString("email"));
            user.setEmail(set.getString("phone"));
            list.add(user);
        }
        return list;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
}
